<?php

class Tuan extends CI_Model
{

    public function __construct()
    {
        // Call the CI_Model constructor
        parent::__construct();
        $this->load->database();
        date_default_timezone_set('PRC');
    }

    /**
     * 根据微信id获取已参的团
     *
     * @param $open_id
     * @return mixed
     */
        public function get_tuan($data,$params = 'tuan.*', $start = null,$limit = null )
        {
            $this->db->select($params);
            $this->db->from('tuan');
            $this->db->join('weixin_mobile', 'weixin_mobile.open_id = tuan.open_id', 'left');
            $this->db->where($data);
            if($limit !== null && $start !== null){
                $this->db->limit($limit, $start);
            }
            $query = $this->db->get();
            return $query->result_array();
        }

    /**
     * 获取团成员
     *
     * @param $open_id
     * @return mixed
     */
    public function get_tuan_member($data, $params = 'tuan.*')
    {
        $this->db->select($params);
        $this->db->from('tuan');
        $this->db->join('tuanyuan', 'tuan.id = tuanyuan.tuan_id', 'left');
        $this->db->join('weixin_mobile', 'weixin_mobile.open_id = tuanyuan.open_id', 'left');
        $this->db->where($data);
        $this->db->order_by('tuanyuan.create_time','asc');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * 获取用户手机号
     *
     * @param $open_id
     * @return mixed
     */
    public function get_weixin_mobile($data)
    {
        $query = $this->db->get_where('weixin_mobile', $data);
        return $query->row_array();
    }

    /**
     * 添加团
     *
     * @param $data
     * @return mixed
     */
    public function insert_tuan($data)
    {
        $this->db->insert('tuan',$data);
        return $this->db->insert_id();
    }

    /**
     * 添加团员
     *
     * @param $data
     * @return mixed
     */
    public function insert_tuanyuan($tuan_id,$data,$empty_user = false, $user_data = null)
    {
        $this->db->trans_start();
        $query = $this->db->get_where('tuanyuan', array('tuan_id' => $tuan_id));
        $count = $query->result_array();
        if(count($count) == 9){
            $this->db->trans_rollback();
            return false;
        }
        $this->db->insert('tuanyuan',$data);
        //满团
        if(count($count) == 7){
            $this->db->where(array('id' => $tuan_id));
            $this->db->update('tuan', array('status' => 1));
        }
        if($empty_user){
            $this->insert_m_wx($user_data);
        }
        $this->db->trans_complete();
        return true;
    }

    /**
     * 添加用户
     *
     * @param $data
     * @return mixed
     */
    public function insert_m_wx($data)
    {
        return $this->db->insert('weixin_mobile', $data);
    }


    /**
     * 获取用户手机号
     *
     * @param $open_id
     * @return mixed
     */
    public function get_tuanyuans($data)
    {
        $query = $this->db->get_where('tuanyuan', $data);
        return $query->result_array();
    }
}